package com.example.springDemo.controller.小项目;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springDemo.bean.Login;
import com.example.springDemo.bean.LoginUser;
import com.example.springDemo.bean.User;
import com.example.springDemo.exception.自定义异常的自定义异常处理;
import com.example.springDemo.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * templates这个静态文件  都是经过处理的 html文件的访问不需要加   .html
 */
@Controller
public class thymeleafController {


    @Autowired
    LoginService loginService;

    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping("/toThymeleaf")
    public String toThymeleaf(Model model){

        //model中的数据会被放在请求域中request. setAttribute("a", aa)
        model.addAttribute("msg","你好thymeleaf");
        model.addAttribute("link", "http://www.baidu.com");

        return "thymeleafDemo";
    }

    @GetMapping("/login")
    public String loginDemo(){
        return "login";
    }

    @PostMapping("/login")
    public String index(LoginUser loginUser, HttpSession session, Model model){

        if("1".equals(loginUser.getUsername())&&StringUtils.hasLength(loginUser.getPassword())){
            session.setAttribute("loginUser",loginUser);//登录成功的用户保存一下
            return "redirect:/index_html";//重定向 防止表单重复提交
//            response.sendRedirect("/index_html");底层用的是这个
//            代表请求的request         代表响应response
        }else {
            model.addAttribute("msg","账号密码错误");
            return "login";//回到登录界面
        }
    }

    /**
     * //ModelAndView  最后信息几乎都在ModelAndView存着  并进行操作
     *     //View：定义了页面的渲染逻辑
     *
     * 便利所有的视图解析器  找到所需的
     *
     *
     *  视图解析：
     * "redirect:/index_html";//重定向 防止表单重复提交
     *    response.sendRedirect("/index_html");底层用的是这个
     * "forward:/success";//转发到/success请求
     *   ○ new InternalResourceView(forwardUrl); -->
     *   转发request.getRequestDispatcher(path).forward(request, response);
     *
     * 可以自定义视图解析器+自定义试图
     *
     */

    @GetMapping("/index_html")
    public String goIndex(HttpSession session, Model model){
        //判断是否登录  这个太麻烦了  使用拦截器
        if (session.getAttribute("loginUser") != null) {
            ValueOperations valueOperations = redisTemplate.opsForValue();
            model.addAttribute("indexCount",valueOperations.get("/index_html"));
            model.addAttribute("sqlCount",valueOperations.get("/sql"));

//            System.out.println(valueOperations.get("/error"));
//            System.out.println(valueOperations.get("/index_html"));
//            for (String s:redisTemplate.keys("*")){//获取所有的key
//                System.out.println(s);
//            }
            return "index";
        }else {
            model.addAttribute("msg","请登录");
            return "login";//回到登录界面
        }
    }

    @GetMapping("/dynamic_table")
    public String dynamic_table(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
//         List<LoginUser> loginUsers = Arrays.asList(new LoginUser("张1", "666"),
//                new LoginUser("张22", "666"),
//                new LoginUser("张3", "666"),
//                new LoginUser("张4", "666"),
//                new LoginUser("张5", "666"));
//
//        if(loginUsers.size()>3){
//            throw new 自定义异常的自定义异常处理();
//        }
//        List<Login> list = loginService.list();
//        model.addAttribute("logins",list);
        Page<Login> logins = new Page<>(pn, 2);
        Page<Login> page = loginService.page(logins, null);
        model.addAttribute("page",page);

        return "table/dynamic_table";
    }
    @GetMapping("/user/delete/{id}")
    public String deleteLogin(@PathVariable("id") Long id,
                              @RequestParam(value = "pn" ,defaultValue = "1") Integer pn,
                              RedirectAttributes ra){
        loginService.removeById(id);
        ra.addAttribute("pn",pn);
        return "redirect:/dynamic_table";//重定向
    }

}
